package com.ruoyi.gwc.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SnowFlakeIDGenerator;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gwc.domain.TEtcManagement;
import com.ruoyi.gwc.domain.TFuelCardManagement;
import com.ruoyi.gwc.mapper.TEtcManagementMapper;
import com.ruoyi.gwc.mapper.TFuelCardManagementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gwc.mapper.TCostRecordMapper;
import com.ruoyi.gwc.domain.TCostRecord;
import com.ruoyi.gwc.service.ITCostRecordService;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 费用记录Service业务层处理
 * 
 * @author wp
 * @date 2024-12-13
 */
@Service
public class TCostRecordServiceImpl extends ServiceImpl<TCostRecordMapper, TCostRecord>  implements ITCostRecordService
{
    @Autowired
    private TCostRecordMapper tCostRecordMapper;
    @Autowired
    private TFuelCardManagementMapper tFuelCardManagementMapper;
    @Autowired
    private TEtcManagementMapper tEtcManagementMapper;

    /**
     * 查询费用记录
     * 
     * @param recordId 费用记录主键
     * @return 费用记录
     */
    @Override
    public TCostRecord selectTCostRecordByRecordId(Long recordId)
    {
        return tCostRecordMapper.selectTCostRecordByRecordId(recordId);
    }

    /**
     * 查询费用记录列表
     * 
     * @param tCostRecord 费用记录
     * @return 费用记录
     */
    @Override
    public List<TCostRecord> selectTCostRecordList(TCostRecord tCostRecord)
    {
        return tCostRecordMapper.selectTCostRecordList(tCostRecord);
    }

    /**
     * 新增费用记录
     * 
     * @param tCostRecord 费用记录
     * @return 结果
     */
    @Override
    public int insertTCostRecord(TCostRecord tCostRecord)
    {
        tCostRecord.setCreateTime(DateUtils.getNowDate());
        tCostRecord.setCreateBy(getUsername());
        tCostRecord.setRecordId(SnowFlakeIDGenerator.getInstance().nextId());
        if ( StringUtils.isEmpty(tCostRecord.getRemarks()) ){
            tCostRecord.setRemarks("充值记录");
        }
        //费用名称 0加油费1停车费2过路费3维修费4保养费5保险费6其他费用7etc费用
        if (tCostRecord.getCostName()==0){
            TFuelCardManagement tFuelCardManagement =  new TFuelCardManagement();
            tFuelCardManagement.setFuelId(tCostRecord.getFuelId());
            tFuelCardManagement.setAmount(tCostRecord.getAmount());
            tFuelCardManagementMapper.updateTFuelCardManagement(tFuelCardManagement);
        }else if (tCostRecord.getCostName()==7){
            TEtcManagement tEtcManagement = new TEtcManagement();
            tEtcManagement.setEctId(tCostRecord.getEctId());
            tEtcManagement.setAmount(tCostRecord.getAmount());
            tEtcManagementMapper.updateTEtcManagement(tEtcManagement);
        }

        return tCostRecordMapper.insertTCostRecord(tCostRecord);
    }

    /**
     * 修改费用记录
     * 
     * @param tCostRecord 费用记录
     * @return 结果
     */
    @Override
    public int updateTCostRecord(TCostRecord tCostRecord)
    {
        tCostRecord.setUpdateTime(DateUtils.getNowDate());
        tCostRecord.setUpdateBy(getUsername());
        return tCostRecordMapper.updateTCostRecord(tCostRecord);
    }

    /**
     * 批量删除费用记录
     * 
     * @param recordIds 需要删除的费用记录主键
     * @return 结果
     */
    @Override
    public int deleteTCostRecordByRecordIds(Long[] recordIds)
    {
        return tCostRecordMapper.deleteTCostRecordByRecordIds(recordIds);
    }

    /**
     * 删除费用记录信息
     * 
     * @param recordId 费用记录主键
     * @return 结果
     */
    @Override
    public int deleteTCostRecordByRecordId(Long recordId)
    {
        return tCostRecordMapper.deleteTCostRecordByRecordId(recordId);
    }
}
